/**
 * @file field
 * @author fanxueliang@qq.com
 */
import {map} from 'lodash-es';
import type {AlternativeSpecItem, FieldDatasource, SelectObjectField, EnumValuesSpecItem} from '../interface';
import {getBasicField} from '../util';

export function createAlternativeField(spec: AlternativeSpecItem, showIndex: number, path: string) {
    const enumValues = spec.enumValues;
    const datasource = map<EnumValuesSpecItem, FieldDatasource>(enumValues, (enumItem) => {
        const data = {
            label: enumItem.displayName,
            value: enumItem.value,
            hidden: enumItem[':hidden']
        };
        return data;
    });

    const basicField = getBasicField(spec, path, showIndex);
    return {
        ...basicField,
        type: 'OBJECT',
        isLeaf: true,
        spec,
        component: 'Select',
        children: [],
        datasource
    } as SelectObjectField;
}
